package com.doge.service.admin.log;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.log.Log;
import com.doge.bean.pojo.log.LogExample;
import com.doge.mapper.LogMapper;
import com.doge.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: doge
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-29 20:54
 **/

@Service
public class LogServiceImpl implements LogService{
    @Autowired
    LogMapper mapper;

    @Override
    public BaseData list(BaseParam param, String name) {
        // 分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        // 模糊查询
        LogExample example = new LogExample();
        LogExample.Criteria criteria = example.createCriteria().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(name)){
            criteria.andAdminLike("%"+name+"%");
        }
        // 按照时间逆序排列（从最近到最远）
        example.setOrderByClause("add_time desc");
        // 获得 List<Log>
        List<Log> logs = mapper.selectByExample(example);
        // 封装返回
        PageInfo pageInfo = new PageInfo(logs);
        BaseData data = BaseData.list(logs, pageInfo.getTotal());
        data.setPages(pageInfo.getPages());
        data.setPage(param.getPage());
        data.setLimit(param.getLimit());
        return data;
    }
}
